/*
 * @Author: 李超越 1509572300@qq.com
 * @Date: 2024-07-28 13:09:34
 * @LastEditors: 李超越 1509572300@qq.com
 * @LastEditTime: 2024-07-29 09:20:41
 * @FilePath: /my_git/lesson24_pipe/mypipe.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>



void writer(int wfd)
{
    const char* str="hello father,I am child";
    char buffer[128];
    int cnt=0;
    pid_t pid=0;
    while(1)
    {
        snprintf(buffer,sizeof(buffer),"message:%s,pid:%d,count:%d\n",str,pid,cnt);
        write(wfd,buffer,strlen(buffer));
        cnt++;
        sleep(1);
    }
}

void reader(int rfd)
{
    char buffer[1024];
    while(1)
    {
        sleep(3);
        ssize_t n=read(rfd,buffer,sizeof(buffer)-1);
        (void)n;
        printf("father get a message:%s",buffer);
    }
}

int main()
{
    int pipefd[2];
    int n=pipe(pipefd);
    if(n<0)
    {
        return 1;
    }
    printf("pipefd[0]:%d,pipefd[1]:%d",pipefd[0]/*read*/,pipefd[1]/*write*/);
    pid_t id=fork();
    if(id==0)
    {
        exit(0);
    }
    wait(NULL);
    return 0;
}